package com.soft.mainsite.component.beetl.tag.city;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.soft.mainsite.module.city.dto.CityDto;
import com.soft.mainsite.module.city.service.CityService;
import lombok.extern.slf4j.Slf4j;
import org.beetl.core.GeneralVarTagBinding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * CityInfoTag.java 此类用于根据站点查询城市信息
 *
 * @author:李昭阳
 * @date:2019/5/6 0006
 * @remark:
 */
@Slf4j
@Service
@Scope("prototype")
public class CityInfoTag extends GeneralVarTagBinding {
    @Autowired
    private CityService cityService;

    @Override
    public void render() {
        Integer siteId = Convert.toInt(this.getAttributeValue("siteId"));
        try {
            this.wrapRender(siteId);
        } catch (Exception e) {
            log.error("城市标签渲染失败", e);
        }

    }

    private void wrapRender(int siteId) throws Exception {
        CityDto cityBySiteId = cityService.getCityBySiteId(siteId);
        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(cityBySiteId);
        this.binds(stringObjectMap);
        this.doBodyRender();
    }
}
